﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Service;
using Model;
using System.Data;
using System.Data.SqlClient;
namespace Manager
{
    /// <summary>
    /// HW_OrgUser业务逻辑
    /// 创建人：Wilson
    /// 创建时间：2013-1-29  
    /// </summary>
    public class HW_OrgUserManager
    {
        			
        #region##通过主键ID得到HW_OrgUser
        /// <summary>
        /// 功能：通过ID得到HW_OrgUser
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
        public HW_OrgUser GetById(string xx_username)
        {            
            using (HW_OrgUserService ws = new HW_OrgUserService())
			{
				return ws.GetById(xx_username);
			}
        }
        public string GetMaxUser(string dis)
        {
            using (HW_OrgUserService ws = new HW_OrgUserService())
            {
                return ws.GetMaxUser(dis);
            }
        }
        public string GetMaxUserName(string orgcode)
        {
            using (HW_OrgUserService ws = new HW_OrgUserService())
            {
                return ws.GetMaxUserName(orgcode);
            }
        }
        #endregion
		
		#region##通过主键ID删除HW_OrgUser
        /// <summary>
        /// 功能：通过主键ID删除HW_OrgUser
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(string xx_username)
		{
			using (HW_OrgUserService ws = new HW_OrgUserService())
			{
                SqlConnection conn = new SqlConnection(Utility.WebConn.connString);
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                SqlTransaction trans = conn.BeginTransaction();

                HW_XXX_UserService ser = new HW_XXX_UserService();
                try
                {
                    ws.DeleteById(xx_username);
                    int n= ser.DeleteById(xx_username);
                    return n;
                }
                catch (Exception e)
                {
                    trans.Rollback();
                    throw e;
                }
                finally
                {
                    conn.Close();
                }
			}
		}
        public List<HW_OrgUser> GetByOrgCode(string orgcode)
        {
            using (HW_OrgUserService ws = new HW_OrgUserService())
            {
                return ws.GetByOrgCode(orgcode).ToList();
            }
        }
		#endregion			
		
        #region##添加HW_OrgUser
        /// <summary>
        /// 功能：添加HW_OrgUser
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(HW_OrgUser hw_orguser)
        {
            using (HW_OrgUserService ws = new HW_OrgUserService())
			{
                SqlConnection conn=new SqlConnection (Utility.WebConn.connString);
                if(conn.State==ConnectionState.Closed)
                    conn.Open();
                SqlTransaction trans=conn.BeginTransaction();
                HW_XXX_UserService userService = new HW_XXX_UserService();
                HW_XXX_User user=new HW_XXX_User ()
                {
                    XX_UserName=hw_orguser.XX_UserName,
                    XX_Pass=hw_orguser.XX_Pass,
                    RoleId=2000,
                    CNName=hw_orguser.CNName,
                };
                try
                {
                    userService.Add(trans, user);
                    int n= ws.Add(trans, hw_orguser);
                    trans.Commit();
                    return n;
                }
                catch(Exception e)
                {
                    trans.Rollback();
                    throw e;
                }
                finally
                {
                    conn.Close();
                }
			}
        }
        #endregion
		
		#region##得到HW_OrgUser集合
		/// <summary>
        /// 功能：得到HW_OrgUser集合
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>       
        /// <returns></returns>
		public IList<HW_OrgUser> GetList()
		{
		    using (HW_OrgUserService ws = new HW_OrgUserService())
			{
				return ws.GetList();
			}
		}
        public DataTable GetList(int pageIndex, int pageSize, out int count)
        {
            using (HW_OrgUserService ws = new HW_OrgUserService())
            {
                return ws.GetList(pageIndex, pageSize, out count);
            }
        }
		#endregion
		
		#region##修改HW_OrgUser
        /// <summary>
        /// 功能：修改HW_OrgUser
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public void ModHW_OrgUser(HW_OrgUser hw_orguser)
        {
            using (HW_OrgUserService ws = new HW_OrgUserService())
			{
                System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(Utility.WebConn.connString);
                if (conn.State == System.Data.ConnectionState.Closed)
                    conn.Open();
                System.Data.SqlClient.SqlTransaction trans = conn.BeginTransaction();
                try
                {
                    ws.ModHW_OrgUser(trans,hw_orguser);
                    trans.Commit();
                }
                catch
                {
                    trans.Rollback();
                }
			}
        }
        public void ResetPassWord(HW_OrgUser hw_orguser)
        {
            using (HW_OrgUserService ws = new HW_OrgUserService())
            {
                System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(Utility.WebConn.connString);
                if (conn.State == System.Data.ConnectionState.Closed)
                    conn.Open();
                System.Data.SqlClient.SqlTransaction trans = conn.BeginTransaction();
                try
                {
                    HW_XXX_UserService service = new HW_XXX_UserService();
                    service.ResetPassword(trans, hw_orguser.XX_UserName, hw_orguser.XX_Pass);
                    ws.ResetPassword(trans, hw_orguser.XX_UserName,hw_orguser.XX_Pass);
                    trans.Commit();
                }
                catch
                {
                    trans.Rollback();
                }
            }
        }
        #endregion
		
		#region##统计HW_OrgUser
		/// <summary>
        /// 修改HW_OrgUser
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
		public int SelectCount()
		{
			using (HW_OrgUserService ws = new HW_OrgUserService())
			{
				return ws.SelectCount();
			}
		}
        /// <summary>
        /// 修改HW_OrgUser
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int SelectCountById()
        {
            using (HW_OrgUserService ws = new HW_OrgUserService())
			{
				return ws.SelectCountById();
			}
        }
		#endregion
        #region##自定义HW_OrgUser
        //public DataTable Search(int pageIndex, int pageSize, out int count,string searchString)
        //{
        //    //using (HW_OrgUserService ws = new HW_OrgUserService())
        //    //{
        //    //    return ws.Search(pageIndex, pageSize, out count, searchString);
        //    //}
        //}
        public DataTable GetSubList(int pageIndex, int pageSize, out int count, string pdistrictCode)
        {
            using (HW_OrgUserService ws = new HW_OrgUserService())
            {
                return ws.GetSubList(pageIndex, pageSize, out count, pdistrictCode);
            }
        }
        public HW_OrgUser GetByUserName(string userName)
        {
            using (HW_OrgUserService ws = new HW_OrgUserService())
            {
                return ws.GetByUserName(userName);
            }
        }
        public DataTable Search(string district, string searchString)
        {
            using (HW_OrgUserService ws = new HW_OrgUserService())
            {
                return ws.Search(district, searchString);
            }
        }
        #endregion
    }
}